<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/22
 * Time: 15:22
 */


namespace zhtfrontend\controllers;

use common\components\actions\DeleteAction;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use doumiaoduo\buildform\BuildForm;
use zhtfrontend\models\datas\Admin;
use zhtfrontend\models\datas\ChannelDistribution;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\widgets\ActiveForm;

class ChanneldistributionController extends \zhtfrontend\components\bases\BaseController
{
    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
            $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
            $data = ChannelDistribution::find()->orderBy('id ASC');
            if (!empty($param['searchData'])) {
                $search_data = array_filter($param['searchData']);
                if (!empty($search_data['name'])) {
                    $data->andWhere(['like', 'name', $search_data['username']]);
                }
                if (!empty($search_data['contacts'])) {
                    $data->andWhere(['like', 'contacts', $search_data['name']]);
                }
                if (!empty($search_data['mobile'])) {
                    $data->andWhere(['mobile'=> $search_data['mobile']]);
                }
            }
            $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
            isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
            $model = $data->offset($pages->offset)->limit($pages->limit)->all();
            $array = Fun::asArray($model, ['operate']);
        return $this->render('index',['data'=>$array,'pages'=>$pages]);
    }

    public function actionCreate()
    {
        $this->layout=false;
        $model = new ChannelDistribution();
        $admin = Admin::find()->where(['role'=>'class_teacher'])->all();
        $admin = ArrayHelper::map($admin,'id','name');
        $form = new BuildForm('@app/components/forms/channeldistributionForm.php');
//        $form->setItems('channeldistribution', 'admin_id', $admin);
        $form->loadModel(['channeldistribution' => $model]);
        $form->loadDefaultValues();
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'channeldistribution-form' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $admin_name = Admin::findOne($model->admin_id);
            $model->admin = $admin_name->name;
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '渠道添加成功'), Fun::url(['channeldistribution/index']));
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public function actionUpdate()
    {
        $this->layout=false;
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '错误请求'));
        }
        $model = ChannelDistribution::findOne(\Yii::$app->request->get('id'));
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $admin = Admin::find()->where(['role'=>'class_teacher'])->all();
        $admin = ArrayHelper::map($admin,'id','name');
        $form = new BuildForm('@app/components/forms/channeldistributionForm.php');
//        $form->setItems('channeldistribution', 'admin_id', $admin);
        $form->loadModel(['channeldistribution' => $model]);
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'channeldistribution-form' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $admin_name = Admin::findOne($model->admin_id);
            $model->admin = $admin_name->name;
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '渠道修改成功'), Fun::url(['admin/index']));
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public function actions()
    {
        return [
            'delete' => [
                'class' => DeleteAction::className(),
                'modelClass' => ChannelDistribution::className(),
                'message' => ['success' => '渠道删除成功！'],
                'redirectTo' => 'channeldistribution/index',
            ],
        ];
    }
}
